//
//  TrackDrawingView.m
//  student
//
//  Created by JCai on 2018/10/25.
//  Copyright © 2018 higer. All rights reserved.
//

#import "TrackDrawingView.h"
#import "ExamPlayInfoModel.h"

@interface TrackDrawingView ()

@property (nonatomic, assign) NSInteger lastSelect;
@property (nonatomic, strong) NSArray *pathsArray;

@property (nonatomic, strong) CAShapeLayer *lineLayer;
@property (nonatomic, strong) UIImageView *carImageView;


@end


@implementation TrackDrawingView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        
        self.lastSelect = 0;
        
        self.lineLayer = [CAShapeLayer layer];
        self.lineLayer.lineWidth = 2;
        self.lineLayer.strokeColor = [UIColor redColor].CGColor;
        self.lineLayer.fillColor = nil; // 默认为blackColor
        [self.layer addSublayer:self.lineLayer];
        
        _carImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 51)];
        [_carImageView setImage:[UIImage imageNamed:@"TeachSimulationCar"]];
        [self addSubview:_carImageView];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
//    [super drawRect:rect];
    //设置颜色
//    [[UIColor redColor] set];
//

    NSLog(@"%@_____%@", @(self.lastSelect), @(self.pathSelected));
    self.lastSelect = self.pathSelected;
//    NSLog(@"%@____%@", @(playInfo.pox), @(playInfo.poy));
}



#pragma mark -
- (void)reloadDrawing:(NSArray *)paths
{
    self.pathsArray = paths;
    
//    self.pathSelected = 0;
//    self.startTime = CFAbsoluteTimeGetCurrent();
//    [self startTimer];
    
//    CGPoint tempPoint = CGPointMake(300, 300);
//    for (NSInteger i=0; i<paths.count-1; i++) {
//        UIBezierPath *path=[UIBezierPath bezierPath];
//        [path setLineWidth:1];
//
//        [path moveToPoint:CGPointMake(temp.pox, temp.poy)];
//        [path addLineToPoint:tempPoint];
//        [_pathsArray addObject:path];
//
////        [self performSelectorOnMainThread:@selector(reloadLineLayer) withObject:nil waitUntilDone:NO];
////        [NSThread sleepForTimeInterval:0.05];
//    }
////    [self performSelectorInBackground:@selector(reloadLine) withObject:nil];
}

- (void)reloadLineLayer:(NSInteger)pathSelect
{
    self.pathSelected = pathSelect;
    
    ExamPlayInfo *playInfo = [self.pathsArray firstObject];
    
    // 线的路径
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    // 起点
    [linePath moveToPoint:CGPointMake(playInfo.pox, playInfo.poy)];
    // 其他点
    for (NSInteger i=0; i<self.pathSelected; i++) {
        playInfo = [self.pathsArray objectAtIndex:i];
        [linePath addLineToPoint:CGPointMake(playInfo.pox, playInfo.poy)];
    }
    
    self.lineLayer.path = linePath.CGPath;

    WS(ws);
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        ws.carImageView.transform = CGAffineTransformMakeRotation(playInfo.angle * M_PI / 180.0f);
        [ws.carImageView setCenter:CGPointMake(playInfo.pox, playInfo.poy)];
    } completion:^(BOOL finished) {
        
    }];


}

@end
